#include "sdlevent.h"
#include <iostream>
#include <SDL.h>
#include <stdio.h>

SDLEvent::SDLEvent() {}

#define MY_QUIT_EVENT (SDL_USEREVENT + 2)

void SDLEvent::sdlEvent()
{
    SDL_Window* window = NULL;
    SDL_Event event;

    int event_exit = 0;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("SDL事件处理",SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,800,600,SDL_WINDOW_SHOWN);

    while(1)
    {
        SDL_WaitEvent(&event);

        switch (event.type) {
        case SDL_KEYDOWN:
            switch (event.key.keysym.sym) {
            case SDLK_a:
                std::cout << "Press A" << std::endl;
                break;
            case SDLK_s:
                std::cout << "Press S" << std::endl;
                break;
            case SDLK_d:
                std::cout << "Press D" << std::endl;
                break;
            case SDLK_w:
                break;
            case SDLK_q:
                SDL_Event event_q;
                event_q.type = MY_QUIT_EVENT;
                SDL_PushEvent(&event_q);
                break;
            default:
                printf("Press 0x%x\n",event.key.keysym.sym);
                break;
            }
        break;
        case SDL_MOUSEBUTTONDOWN:
            if(event.button.button == SDL_BUTTON_LEFT)
            {
                std::cout << "SDL_Button_left" << std::endl;
            }
            else if(event.button.button == SDL_BUTTON_RIGHT)
            {
                std::cout << "SDL_Button_right" << std::endl;
            }
            else
            {
                printf("Press Mouse Button %d\n",event.button.button);
            }
        break;
        case SDL_MOUSEMOTION:
            printf("Mouse Moved To (%d,%d)\n",event.button.x,event.button.y);
        break;
        case MY_QUIT_EVENT:
            std::cout <<"MY_QUIT_EVENT" << std::endl;
            event_exit = 1;
            break;
        }
        if(event_exit) break;
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
}
